package org.nepu.scr.indexrate.ratecompliance.compliancescore;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.lang.Validator;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.lboot.core.domain.ResponseDTO;
import org.lboot.core.exception.BusinessException;
import org.lboot.jpa.utils.JpaDataUtil;
import org.lboot.ufs.service.UfsCtl;
import org.nepu.scr.indexrate.ratecompliance.compliance.params.ComplianceCreateParams;
import org.nepu.scr.indexrate.ratecompliance.compliance.params.ComplianceQueryParams;
import org.nepu.scr.indexrate.ratecompliance.compliancescore.params.ComplianceScoreData;
import org.nepu.scr.indexrate.ratecompliance.compliancescore.params.ComplianceScoreUpdateParams;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.http.HttpStatus;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.persistence.criteria.Predicate;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("score")
@AllArgsConstructor
@Api(tags="符合性评级比较矩阵")
public class ComplianceScoreController {

    ComplianceScoreService complianceService;

    UfsCtl ufsCtl;

//    @AuthLogin
//    @PostMapping("compliances")
//    @ApiOperation(value = "新建指标比较矩阵")
    public ResponseDTO<Object> dataCreate(@Validated @RequestBody ComplianceCreateParams params){

        if(params.getComplianceId().equals("")){
            throw new BusinessException(HttpStatus.BAD_REQUEST,"评价任务ID不能为空");
        }

        ComplianceScoreData data = new ComplianceScoreData();
        List<ComplianceScore> dataList = complianceService.getByComplianceName(params.getComplianceId());



        if (dataList.size() > 0){
            throw new BusinessException(HttpStatus.BAD_REQUEST,"存在指标评价任务"+params.getComplianceId());
        }

        ComplianceScore compliance = new ComplianceScore();
        compliance.setComplianceName(params.getComplianceId());
        data.setComplianceId(params.getComplianceId());
        ComplianceScore e = complianceService.saveOne(compliance);
        String complianceDataId = e.getId();
        complianceService.saveComplianceData(complianceDataId, data);

        return ResponseDTO.succData(
                e
        );
    }

//    @DeleteMapping("compliances/{id}")
//    @ApiOperation(value = "删除指标比较矩阵")
    public String dataDelete(@PathVariable("id") String id){
        complianceService.deleteComplianceData(id);
        complianceService.deleteOne(id);
        return "删除成功";
    }

    @GetMapping("compliances")
    @ApiOperation(value = "指标比较矩阵列表")
    public ResponseDTO<Object> dataList(ComplianceQueryParams params){
        Pageable pageable = JpaDataUtil.buildPageable(params);
        if (Validator.isEmpty(params.getSearchKey())){
            ComplianceScore data = new ComplianceScore();
            BeanUtil.copyProperties(params,data, CopyOptions.create().setIgnoreNullValue(true).setIgnoreError(true));
            Example<ComplianceScore> example = Example.of(data);
            return ResponseDTO.succData(
                    complianceService.getAll(pageable,example)
            );
        }else {
            Specification<ComplianceScore> spec = (root, query, cb) -> {
                List<Predicate> list = new ArrayList<>();
                Predicate p1 = cb.like(root.get("complianceId").as(String.class),"%"+params.getSearchKey()+"%");
                list.add(p1);
                Predicate[] predicates = new Predicate[list.size()];
                return cb.or(list.toArray(predicates));
            };
            return ResponseDTO.succData(
                    complianceService.getAll(pageable, spec)
            );
        }
    }

    @GetMapping("compliances/{id}")
    @ApiOperation(value = "比较矩阵查询")
    public ResponseDTO<Object> dataQuery(@PathVariable("id") String id){
        ComplianceScoreData complianceData = complianceService.readComplianceData(id);
        return ResponseDTO.succData(
                complianceData
        );
    }

    @PutMapping("compliances/{complianceId}/data")
    @ApiOperation(value = "比较矩阵更新")
    public ResponseDTO<Object> dataUpdate(@PathVariable("complianceId") String complianceId, @Validated @RequestBody ComplianceScoreUpdateParams params){
        ComplianceScoreData data = complianceService.readComplianceData(complianceId);
        BeanUtil.copyProperties(params,data, CopyOptions.create().setIgnoreNullValue(true).setIgnoreError(true));
        return ResponseDTO.succData(
                complianceService.saveComplianceData(complianceId, data)
        );
    }
}
